最初のPOSTリクエスト
このチュートリアルでは、0rbit
プロセスでPOSTリクエストを行う方法を学びます。
🔑 前提条件
- システムにaosがインストールされていること。
- 一部の$0RBT。こちらで$0RBTを入手する方法を学ぶこちら_
- 任意のコードエディタ(VSCode、Sublime Textなど)
上記の前提条件が整ったら、
🛠️ 開発を始めましょう
プロジェクトの初期化
プロジェクトディレクトリに0rbit-Post-Request.lua
という新しいファイルを作成します。
touch 0rbit-Post-Request.lua
変数の初期化
local json = require("json")
_0RBIT = "BaMK1dfayo75s3q1ow6AO64UDpD9SEFbeE8xYrY2fyQ"
_0RBT_POINTS = "BUhZLMwQ6yZHguLtJYA5lLUa9LQzLXMXRfaq9FVcPJc"
FEE_AMOUNT = "1000000000000" -- 1 $0RBT
BASE_URL = "https://arweave.dev/graphql"
-- The data body to be sent in the POST request
BODY = json.encode({
query = [[
query {
transactions(
owners: ["vh-NTHVvlKZqRxc8LyyTNok65yQ55a_PJ1zWLb9G2JI"]
) {
edges {
node {
id
}
}
}
}
]]
});
ReceivedData = ReceivedData or {}
リクエストの送信
以下のコードには、0rbit
プロセスに1 $0RBTを送信し、BASE_URL
に対してPOSTリクエストを行うハンドラが含まれています。
Handlers.add(
"Post-Request",
Handlers.utils.hasMatchingTag("Action", "First-Post-Request"),
function(msg)
Send({
Target = _0RBT_TOKEN,
Action = "Transfer",
Recipient = _0RBIT,
Quantity = FEE_AMOUNT,
["X-Url"] = BASE_URL,
["X-Action"] = "Post-Real-Data",
["X-Body"] = BODY
})
print(Colors.green .. "You have sent a POST Request to the 0rbit process.")
end
)
上記のコードの内訳:
Handlers.add
は、ao
プロセスに新しいハンドラを追加するために使用されます。Post-Request__はハンドラの名前です。
Handlers.utils.hasMatchingTag
は、受信メッセージがFirst-Post-Requestと同じタグを持っているかをチェックする関数です。function(msg)
は、ハンドラが呼び出されたときに実行される関数です。Send
は、いくつかのタグを引数として取り、ao上にメッセージを作成する関数です。Tag Description Target The processId of the recipient. In this case, it's the $0RBT token processId. Action The tag that defines the handler to be called in the recipient process. In this case it's Transfer
Recipient The tag that accepts the processId to whom the $0RBT will be sent. In this case, it's the 0rbit processId. Quantity The amount of $0RBT to be sent. ["X-Url"] The forwarded-tag which contains the URL and the same will be used by the 0rbit process to fetch the data. ["X-Action"] The forwarded-tag which contains the action to be performed by the 0rbit process. In this case, it's Post-Real-Data. ["X-Body"] The forwarded-tag which contains the data body to be sent in the POST request.
データの受信
以下のコードには、0rbit
プロセスからデータを受信して印刷するハンドラが含まれています。
Handlers.add(
"Receive-Data",
Handlers.utils.hasMatchingTag("Action", "Receive-Response"),
function(msg)
local res = json.decode(msg.Data)
ReceivedData = res
print(Colors.green .. "You have received the data from the 0rbit process.")
end
)
上記のコードの内訳:
Handlers.add
は、ao
プロセスに新しいハンドラを追加するために使用されます。- Receive-Dataはハンドラの名前です。
Handlers.utils.hasMatchingTag
は、受信メッセージがReceive-Responseと同じタグを持っているかをチェックする関数です。function(msg)
は、ハンドラが呼び出されたときに実行される関数です。json.decode
は、受信したJSONデータをデコードするために使用されます。ReceivedData = res
は、受信したデータをReceivedData
変数に格納します。
0rbitプロセスは常にデータをstring
形式で送信します。
上記では、受信データが文字列化されたJSONであるためjson.decode
が使用されています。
そのため、要件に応じてデータをデコードする必要があります。
🏃 プロセスを実行する
新しいプロセスを作成し、スクリプトを読み込む
aos 0rbitPostRequest --load 0rbit-Post-Request.lua
上記のコマンドは、0rbitPostRequestという名前の新しいプロセスを作成し、0rbit-Post-Request.lua
を読み込みます。
プロセスに資金を提供する
いくつかの$0RBTをあなたのプロセスIDに転送します。
ハンドラの呼び出し
ハンドラを呼び出して、0rbitプロセスへのリクエストを作成します。
Send({ Target= ao.id, Action="First-Post-Request" })
データの確認
ReceivedData
変数に格納されたデータを確認するには、以下のコマンドを実行します:
ReceivedData
成功裏に実行されると、ターミナルにJSONデータが表示されます:
{
data = {
transactions = {
edges = {
{
node = {
id = "nH0NU9rgNqGVHwjtjFvnIyXpsP7YVrj_v7JxFErHNB4"
}
},
//and so on...
{
node = {
id = "9HLUVJo4AcrSxQeapf2hutS2Xp7hx_XDiIvv3vnxDcc"
}
}
}
}
}
}
やった!あなたは成功裏に0rbitプロセスで最初のPOSTリクエストを行いました。🎉
You can find the complete code here:
https://github.com/0rbit-co/examples/blob/main/First-Post-Request.lua